<?php
    class Chat extends CActiveRecord
    {
        public static function model($className = __CLASS__) {
            return parent::model ( $className );
        }

        public function tableName() {                  
            return 'c_chat';
        } 
        public function getChat($currentPage, $numberRecordPerPage,$type,$id)
        {
            $startRecord = ($currentPage - 1) * $numberRecordPerPage;

            $sql = "SELECT * FROM c_chat WHERE type=".$type." AND appId=".$id." ORDER BY create_date DESC LIMIT " . $startRecord . ", " . $numberRecordPerPage;
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryAll();        
            return $rows;
        }

        public function getTotalChat($type,$id)
        {
            $sql = "SELECT count(id) FROM c_chat WHERE type=".$type." AND appId=".$id;
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryRow();        
            return $rows["count(id)"];
        }
        public function checkUserBann($id){
            $sql = "SELECT bann_chat,from_date,to_date FROM c_user WHERE id ='".$id."'";
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryRow();        
            return $rows; 
        }
         public function InsertChat($data)
        {
            $sql = "INSERT INTO c_chat SET 
            userId=:userId
            , username=:username
            , content=:content
            , type=:type
            , appId=:appId
            , create_date=:create_date
            ";
            $command = Yii::app()->db->createCommand($sql);
            $command->bindValues($data);
            $result = $command->execute();
            return $result;
        }
    } 
?>
